<?php
namespace app\command;

use app\beautiful\model\partner\PartnerBonusModel;
use app\beautiful\model\PartnerModel;
use app\beautiful\model\setting\SettingSmsModel;
use app\common\logic\SmsLogic;
use app\common\model\MemberMoneyDetailModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Partnerbonus_com extends Command
{
    protected function configure()
    {
        $this->setName('Partnerbonus_com')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $h=date('H');
        $i=date('i');
        $name = trim($input->getArgument('name'));
        if(($h!='00' || $i!='05') && empty($name)){
            $output->writeln("暂不能执行！每天凌晨05分执行" );exit();
        }
        $page=1;
        $num=100;
        $order['a.partner_bonus_id']='asc';
        do{
            $s=[];
            $s['status']=1;
            $s['end_time']=date('Y-m-d H:i:s');
            $list=PartnerBonusModel::ulist($s,$order,$page,$num);
            if(empty($list['list'])){
                $output->writeln("暂无数据".var_export([$s,$order,$page],true) );exit();
            }
            foreach ($list['list'] as $v){
                $up['status']=2;
                $up['bonus_price']=0;
                $ratio=1;
                if(!empty($v['task_detail']) && $v['task_detail']['status']==1){
                    $ratio=round(($v['cousume_bonus_ratio']+$v['toker_bonus_ratio']+$v['promotion_bonus_ratio'])/100,2);
                }
                $reward_money=round($v['all_reward_money']*($v['bonus_share']/$v['total_share_num'])*$ratio,2);
                $partner_info=PartnerModel::where(['partner_id'=>$v['partner_id']])->find();
                $up['bonus_price']=$reward_money;
                if($reward_money>0){
                    MemberMoneyDetailModel::in($reward_money,1,5,$v['shop_id'],$partner_info['member_id'],$v['partner_id'],$v['partner_bonus_id']);
                    //发送短信通知
                    $sms_setting = SettingSmsModel::where([['shop_id','=',$v['shop_id']]])->find();
                    if(null !== $sms_setting && !empty($v['mobile'])){
                        if($sms_setting->consume_notice==1){
                            $sms_logic= new SmsLogic();
                            $params=[
                                'user'=>(string)$v['nick_name'],
                                'name'=>(string)$v['shop_name'],
                            ];
                            $sms_logic->send($v['shop_id'],[$v['mobile']],$params,'Partnerbonus');
                        }
                    }
                }
                PartnerBonusModel::up($up,['partner_bonus_id'=>$v['partner_bonus_id']]);
            }
//            $page++;
        }while(true);
    }
}